home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1320 / 1320.xpi / components / gmAccount.js next >
Encoding:
Text File  |  2010-01-22  |  11.8 KB  |  439 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. // Global account type
  6. const GLOBAL_TYPE = "global";
  7.  
  8. // Mail account types
  9. const ACCOUNT_TYPE_GMAIL = "gmail";
  10. const ACCOUNT_TYPE_YAHOO = "yahoo";
  11.  
  12. // Password site
  13. const PASSWORD_SITE = "longfocus.gmanager.account";
  14.  
  15. function gmAccount()
  16. {
  17.   // Load the services
  18.   this._logger = Components.classes["@longfocus.com/gmanager/logger;1"].getService(Components.interfaces.gmILogger);
  19. }
  20. gmAccount.prototype = {
  21.   _logger: null,
  22.   _node: null,
  23.   _prefs: null,
  24.   _type: null,
  25.   _email: null,
  26.   _alias: null,
  27.   _password: null,
  28.   _service: null,
  29.   
  30.   get node() { return this._node; },
  31.   get type() { return this._type; },
  32.   get email() { return this._email; },
  33.   get alias() { return this._alias; },
  34.   get password()
  35.   {
  36.     var password = null;
  37.     
  38.     // Check for Toolkit 1.9 (Firefox 3)
  39.     if ("@mozilla.org/login-manager;1" in Components.classes)
  40.     {
  41.       // Lookup the login info
  42.       var loginInfo = this._getLoginInfo();
  43.       
  44.       // Check if the login info exists
  45.       if (loginInfo)
  46.         password = loginInfo.password;
  47.     }
  48.     else
  49.     {
  50.       // Load the password manager service
  51.       var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManagerInternal);
  52.       
  53.       // Initialize the parameters to lookup
  54.       var hostURIFound = {value:""};
  55.       var usernameFound = {value:""};
  56.       var passwordFound = {value:""};
  57.       
  58.       try {
  59.         // Lookup the password for this email
  60.         passwordManager.findPasswordEntry(PASSWORD_SITE, this._email, null, hostURIFound, usernameFound, passwordFound);
  61.       } catch(e) {}
  62.       
  63.       // Check if the password was found
  64.       if (passwordFound)
  65.         password = passwordFound.value;
  66.     }
  67.     
  68.     // Return the password
  69.     return password;
  70.   },
  71.   
  72.   get newMail() { return this.unread > this._lastUnread; },
  73.   get unread()
  74.   {
  75.     var unread = 0;
  76.     
  77.     if (this.getBoolPref("toolbar-unread-count-inbox"))
  78.       unread += this.inboxUnread;
  79.     
  80.     if (this.getBoolPref("toolbar-unread-count-spam"))
  81.       unread += this.spamUnread;
  82.     
  83.     if (this.getBoolPref("toolbar-unread-count-labels"))
  84.     {
  85.       var labels = this.getLabels({});
  86.       
  87.       for (var i = 0; i < labels.length; i++)
  88.         unread += labels[i].unread;
  89.     }
  90.     
  91.     return unread;
  92.   },
  93.   
  94.   get status() { return (this._service ? this._service.status : null); },
  95.   get loggedIn() { return (this._service ? this._service.loggedIn : false); },
  96.   get checking() { return (this._service ? this._service.checking : false); },
  97.   get inboxUnread() { return (this._service ? this._service.inboxUnread : -1); },
  98.   get savedDrafts() { return (this._service ? this._service.savedDrafts : -1); },
  99.   get spamUnread() { return (this._service ? this._service.spamUnread : -1); },
  100.   get spaceUsed() { return (this._service ? this._service.spaceUsed : null); },
  101.   get percentUsed() { return (this._service ? this._service.percentUsed : null); },
  102.   get totalSpace() { return (this._service ? this._service.totalSpace : null); },
  103.   
  104.   getBoolPref: function(aId)
  105.   {
  106.     return this.getCharPref(aId) == "true" ? true : false;
  107.   },
  108.   setBoolPref: function(aId, aValue)
  109.   {
  110.     this.setCharPref(aId, aValue ? "true" : "false");
  111.   },
  112.   
  113.   getCharPref: function(aId)
  114.   {
  115.     var email = (this.type == GLOBAL_TYPE ? "global" : this.email);
  116.     
  117.     if (aId in this._prefs)
  118.     {
  119.       var value = this._prefs[aId].getAttribute("value");
  120.       this._logger.log("(" + email + ") Returning preference: " + aId + " = " + value);
  121.       return value;
  122.     }
  123.     else
  124.       this._logger.log("(" + email + ") Unknown preference: " + aId);
  125.   },
  126.   setCharPref: function(aId, aValue)
  127.   {
  128.     if (aId in this._prefs)
  129.       this._prefs[aId].setAttribute("value", aValue);
  130.   },
  131.   
  132.   getIntPref: function(aId)
  133.   {
  134.     return parseInt(this.getCharPref(aId));
  135.   },
  136.   setIntPref: function(aId, aValue)
  137.   {
  138.     this.setCharPref(aId, aValue ? aValue.toString() : "");
  139.   },
  140.   
  141.   load: function(aNode)
  142.   {
  143.     this._node = aNode;
  144.     this._prefs = new Array();
  145.     
  146.     if (!this._type)
  147.     {
  148.       // Set the account type
  149.       this._type = this._node.getAttribute("type");
  150.       
  151.       // Check if the account type is not global
  152.       if (this._type != GLOBAL_TYPE)
  153.       {
  154.         // Set the account email
  155.         this._email = this._node.getAttribute("email");
  156.         
  157.         // Load the mail service
  158.         switch (this._type)
  159.         {
  160.           case ACCOUNT_TYPE_GMAIL:
  161.             // Create the Gmail mail service
  162.             this._service = Components.classes["@longfocus.com/gmanager/service/gmail;1"].createInstance(Components.interfaces.gmIServiceGmail);
  163.             break;
  164.           case ACCOUNT_TYPE_YAHOO:
  165.             // Create the Yahoo mail service
  166.             // TODO: Coming soon...
  167.             break;
  168.           default:
  169.             break;
  170.         }
  171.         
  172.         // Initialize the mail service
  173.         this.init(this._email);
  174.       }
  175.     }
  176.     
  177.     // Set the account alias
  178.     this._alias = this._node.getAttribute("alias");
  179.     
  180.     // Check if the password attribute is specified
  181.     if (this._node.hasAttribute("password"))
  182.     {
  183.       // Save the account password
  184.       this.savePassword(this._node.getAttribute("password"));
  185.     }
  186.     
  187.     var prefs = this._node.getElementsByTagName("pref");
  188.     
  189.     for (var i = 0; i < prefs.length; i++)
  190.     {
  191.       var pref = prefs.item(i);
  192.       this._prefs[pref.getAttribute("id")] = pref;
  193.     }
  194.   },
  195.   
  196.   savePassword: function(aPassword)
  197.   {
  198.     // Save the password
  199.     this._updatePassword(aPassword);
  200.   },
  201.   
  202.   removePassword: function()
  203.   {
  204.     // Remove the password (if available)
  205.     this._updatePassword(null);
  206.   },
  207.   
  208.   _updatePassword: function(aPassword)
  209.   {
  210.     // Check for Toolkit 1.9 (Firefox 3)
  211.     if ("@mozilla.org/login-manager;1" in Components.classes)
  212.     {
  213.       // Load the login manager service
  214.       var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
  215.       
  216.       // Get the available login info
  217.       var loginInfo = this._getLoginInfo();
  218.       
  219.       // Check if the password is specified
  220.       if (aPassword)
  221.       {
  222.         // Create the updated login info
  223.         var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init");
  224.         var newLoginInfo = new nsLoginInfo(PASSWORD_SITE, "/", null, this._email, aPassword, "", "");
  225.         
  226.         // Check if the login info exists
  227.         if (loginInfo)
  228.           loginManager.modifyLogin(loginInfo, newLoginInfo);
  229.         else
  230.           loginManager.addLogin(newLoginInfo);
  231.       }
  232.       else
  233.       {
  234.         // Check if the login info exists
  235.         if (loginInfo)
  236.           loginManager.removeLogin(loginInfo);
  237.       }
  238.     }
  239.     else
  240.     {
  241.       // Load the password manager service
  242.       var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);
  243.       
  244.       try {
  245.         // Check if the password is specified
  246.         if (aPassword)
  247.           passwordManager.addUser(PASSWORD_SITE, this._email, aPassword);
  248.         else
  249.           passwordManager.removeUser(PASSWORD_SITE, this._email);
  250.       } catch(e) {}
  251.     }
  252.   },
  253.   
  254.   _getLoginInfo: function()
  255.   {
  256.     // Load the login manager service
  257.     var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
  258.     
  259.     // Get all logins that match the site
  260.     var logins = loginManager.findLogins({}, PASSWORD_SITE, "/", null);
  261.     
  262.     // Search for the matching login info
  263.     for (var i = 0; i < logins.length; i++)
  264.     {
  265.       if (logins[i].username == this._email)
  266.         return logins[i];
  267.     }
  268.     
  269.     return null;
  270.   },
  271.   
  272.   /**
  273.    * gmIService
  274.    */
  275.   init: function(aEmail)
  276.   {
  277.     if (this._service)
  278.       this._service.init(aEmail);
  279.   },
  280.   
  281.   getInbox: function(/* Optional */ aPassword)
  282.   {
  283.     if (this._service)
  284.     {
  285.       if (aPassword == undefined)
  286.         aPassword = this.password;
  287.       
  288.       return this._service.getInbox(aPassword);
  289.     }
  290.   },
  291.   
  292.   getCompose: function(/* Optional */ aPassword, aHref)
  293.   {
  294.     if (this._service)
  295.     {
  296.       if (aPassword == undefined)
  297.         aPassword = this.password;
  298.       
  299.       return this._service.getCompose(aPassword, aHref);
  300.     }
  301.   },
  302.   
  303.   login: function(/* Optional */ aPassword)
  304.   {
  305.     if (this._service)
  306.     {
  307.       if (aPassword == undefined)
  308.         aPassword = this.password;
  309.       
  310.       this._lastUnread = 0;
  311.       this._service.login(aPassword);
  312.       this._startTimer();
  313.     }
  314.   },
  315.   
  316.   logout: function()
  317.   {
  318.     if (this._service)
  319.       this._service.logout();
  320.   },
  321.   
  322.   check: function()
  323.   {
  324.     if (this._service)
  325.     {
  326.       this._lastUnread = this.unread;
  327.       this._service.check();
  328.       this._startTimer();
  329.     }
  330.   },
  331.   
  332.   resetUnread: function()
  333.   {
  334.     if (this._service)
  335.     {
  336.       this._service.resetUnread();
  337.       this._startTimer();
  338.     }
  339.   },
  340.   
  341.   getLabels: function(aCount)
  342.   {
  343.     var labels = new Array();
  344.     if (this._service)
  345.       labels = this._service.getLabels({});
  346.     aCount.value = labels.length;
  347.     return labels;
  348.   },
  349.   
  350.   getSnippets: function(aCount)
  351.   {
  352.     var snippets = new Array();
  353.     if (this._service)
  354.       snippets = this._service.getSnippets({});
  355.     aCount.value = snippets.length;
  356.     return snippets;
  357.   },
  358.   
  359.   _startTimer: function()
  360.   {
  361.     this._stopTimer();
  362.     
  363.     if (this.getBoolPref("notifications-check"))
  364.     {
  365.       var interval = (this.getIntPref("notifications-check-interval") * 60000);
  366.       
  367.       if (!isNaN(interval) && interval > 0)
  368.       {
  369.         this._timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
  370.         this._timer.initWithCallback(this, interval, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  371.       }
  372.     }
  373.   },
  374.   
  375.   _stopTimer: function()
  376.   {
  377.     if (this._timer)
  378.     {
  379.       this._timer.cancel();
  380.       this._timer = null;
  381.     }
  382.   },
  383.   
  384.   notify: function(aTimer)
  385.   {
  386.     if (this.loggedIn)
  387.       this.check();
  388.   },
  389.   
  390.   QueryInterface: function(iid)
  391.   {
  392.     if (iid.equals(Components.interfaces.gmIAccount) ||
  393.         iid.equals(Components.interfaces.nsISupports))
  394.       return this;
  395.     throw Components.results.NS_ERROR_NO_INTERFACE;
  396.   }
  397. }
  398.  
  399. var myModule = {
  400.   firstTime: true,
  401.   
  402.   myCID: Components.ID("{d4676ee3-7e3c-455a-b417-37eaea3082ad}"),
  403.   myDesc: "Mail Account",
  404.   myProgID: "@longfocus.com/gmanager/account;1",
  405.   myFactory: {
  406.     createInstance: function(outer, iid) {
  407.       if (outer != null)
  408.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  409.       
  410.       return (new gmAccount()).QueryInterface(iid);
  411.     }
  412.   },
  413.   
  414.   registerSelf: function(compMgr, fileSpec, location, type)
  415.   {
  416.     if (this.firstTime) {
  417.       this.firstTime = false;
  418.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  419.     }
  420.     
  421.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  422.     compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
  423.   },
  424.   
  425.   getClassObject: function(compMgr, cid, iid)
  426.   {
  427.     if (!cid.equals(this.myCID))
  428.       throw Components.results.NS_ERROR_NO_INTERFACE;
  429.     
  430.     if (!iid.equals(Components.interfaces.nsIFactory))
  431.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  432.     
  433.     return this.myFactory;
  434.   },
  435.   
  436.   canUnload: function(compMgr) { return true; }
  437. };
  438.  
  439. function NSGetModule(compMgr, fileSpec) { return myModule; }